import jieba
from wordcloud import WordCloud

# 读取文件
with open('无际涯.txt', 'r', encoding='utf-8') as file:
    s = file.read()

# 中文分词
lst = jieba.lcut(s)

stopword = ['蓬莱有人怜尔苦', '劝尔休休早归去', '精卫精卫我亦劝汝归', '沧海自有变作桑田时'] # 排词器
txt = ''.join(lst)

# 绘制词云图
wordcloud = WordCloud(background_color='white', font_path='msyh.ttc', stopwords=stopword,
                      width=800, height=600)

# 由txt生成词云图
wordcloud.generate(txt)

# 保存成图片
wordcloud.to_file('无际涯词云图.png')
